package com.itheima.d1_polymorphism;

public class Demo02 {
    public static void main(String[] args) {
        // 目标：掌握使用多态的好处，以及多态下的类型转换问题。

        //多态好处
       People p1 = new Student();
        //即可以存放学生对象也可以存放老师对象
        People[] people = new People[3];
        people[0] = new Student();
        people[1] = new Teacher();
        //设计方法，参数使用父类型，调用方法方便，实参可以是父类型和它的子类型
        show(new Student());
        show(new Teacher());

        //多态弊端:父类型不能调用子类型独有的方法
        People p2 = new Teacher();
        People P3 = new Student();
        //p2.study();


        //多态弊端的解决
        //将类型转换为子类型
        Teacher t1 = (Teacher) p2;
        t1.teach();

        Student s1 = (Student) P3;
        s1.study();

        System.out.println("=======");
        People p4 = new Teacher();
        Teacher t2 = (Teacher) p4;
        //Student s4 = (Student) p4;   类型转换错了，就会出现类型转换异常ClassCastException，

        show(new Teacher());
        show(new Student());
    }

    public static void show(People people) {
        //进行类型判断
        if(people instanceof Student){
            Student student = (Student) people;
            student.study();
        }


    }

}
